package com.yinhe.music.yhmusic.start;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.application.MusicApplication;
import com.yinhe.music.yhmusic.base.BaseActivity;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.main.MainActivity;
import com.yinhe.music.yhmusic.network.NetworkManager;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.network.schedulers.SchedulerProvider;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.permission.PermissionReq;
import com.yinhe.music.yhmusic.utils.permission.PermissionResult;
import com.yinhe.music.yhmusic.utils.permission.Permissions;

import io.reactivex.disposables.Disposable;


public class SplashActivity extends BaseActivity {

    Disposable mDisposable;

    long startTime;

    long endTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        startTime = System.currentTimeMillis();
        super.onCreate(savedInstanceState);
        onPermission();
    }

    private void startMusicActivity() {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        intent.putExtras(getIntent());
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {

    }

    private void onPermission() {
        PermissionReq.with(this)
                .permissions(Permissions.STORAGE)
                .result(new PermissionResult() {
                    @Override
                    public void onGranted() {
                        recordUser();
                    }

                    @Override
                    public void onDenied() {
                        showPerMissionDialog();
                    }
                })
                .request();
    }

    private void recordUser() {
        mDisposable = NetworkManager.getWebService().recordUser()
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(loginInfo -> {
                    NetworkManager.getInstance().init();
                    Preferences.setLoginSN(loginInfo.getSn());
                    startMusicActivity();
                }, throwable -> {
                    if (!Preferences.getIsFirst() && !throwable.getMessage().isEmpty()) {
                        Toast.makeText(this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    Preferences.setLoginSN("");
                    Preferences.setFavMenu(0);
                    Preferences.setIsVip(false);
                    DataBaseAccessor.getInstance().uninit();
                    DataBaseAccessor.getInstance().initDBHelper(MusicApplication.getInstance(), "visitor.db");
                    startMusicActivity();
                });
    }


    @Override
    protected void onDestroy() {
        endTime = System.currentTimeMillis();
        LogUtils.i("time:" + (endTime - startTime));
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.onDestroy();
    }

    private void showPerMissionDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("权限申请");
        dialog.setMessage(getString(R.string.no_permission, Permissions.STORAGE_DESC, "扫描本地歌曲"));
        dialog.setPositiveButton("确定", (dialog1, which) -> onPermission());
        dialog.show();
    }
}
